package com.turing.usdtpay.mvp.presenter;

import android.app.Activity;

import com.turing.usdtpay.http.ApiService;
import com.turing.usdtpay.http.ApiUrl;
import com.turing.usdtpay.http.callback.ApiCallback;
import com.turing.usdtpay.http.entity.ResponseDate;
import com.turing.usdtpay.mvp.contract.PhoneContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * @作者：JAY
 * @创建时间：2019/10/29 12:06
 * @描述：
 **/
public class PhonePresenter implements PhoneContract.Present {

    private PhoneContract.View mView;

    public PhonePresenter(PhoneContract.View mView) {
        this.mView = mView;
    }


    @Override
    public void sendBindCode(Activity activity, String phone) {
        HashMap<String ,String> map = new HashMap<>();
        map.put("new_tel",phone);
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_GET_PHONE_CODE_BIND, map, new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    mView.sendSuccess();
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    @Override
    public void sendUnbindCode(Activity activity, String phone) {
        HashMap<String ,String> map = new HashMap<>();
        map.put("del_tel",phone);
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_GET_PHONE_CODE_UNBIND, map, new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    mView.sendSuccess();
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    @Override
    public void bind(Activity activity, String phone, String code) {
        HashMap<String ,String> map = new HashMap<>();
        map.put("new_tel",phone);
        map.put("tel_code",code);
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_PHONE_BIND, map, new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    mView.bindSuccess();
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    @Override
    public void unbind(Activity activity, String phone, String code) {
        HashMap<String ,String> map = new HashMap<>();
        map.put("del_tel",phone);
        map.put("tel_code",code);
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_PHONE_UNBIND, map, new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    mView.unbindSuccess();
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    @Override
    public void getPhone(Activity activity) {
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_GET_PHONE, new HashMap<String, String>(), new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    try {
                        JSONObject object = new JSONObject(date.getDate());
                        String tel = object.optString("tel");
                        mView.getSuccess(tel);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }
}
